﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Text;

namespace InternetSpiderLib.Scenes.Actions.HtmlDataExtractionItems
{
    public class XPathAttributeHtmlDataExtractionItem : IHtmlDataExtractionItem
    {
        public string Key { get; set; }

        public string XPath { get; set; }

        public string AttributeName { get; set; }

        public List<KeyValuePair<string, string>> Extraction(HtmlDocument document)
        {
            List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();

            var childItems = document.DocumentNode.SelectNodes(XPath);
            if (childItems != null)
            {
                foreach (HtmlNode item in childItems)
                {
                    var value = item.Attributes[AttributeName].Value;

                    if (string.IsNullOrEmpty(value) == false)
                        list.Add(new KeyValuePair<string, string>(Key, value));
                }
            }

            return list;
        }
    }
}
